App.controller('checkTaskController', ['$rootScope', '$scope', '$state', 'Page', function ($rootScope, $scope, $state, Page) {


    Page.ajaxPostQuiet('media/bus/companys.do', null, function (resp) {
        $scope.companys = resp.data;
    });


    Page.ajaxPostQuiet('mediaType/all.do', null, function (resp) {
        $scope.mediaTypes = resp.data;
    });


    $scope.checkTaskGrid = {
        column_defs: [
            {
                target: 1,
                template: function(row){
                    return '<a href="javascript:void(0)" ed-click="taskItemDetail">' + row.taskTitle + '</a>';
                }
            },
            {
                target: 4,
                template: function(row){
                    if(row.percent * 100 == 0) {
                        //return '<span>0%</span>'
                        return '<div class="progress progress-striped" style="margin-bottom: 0"> ' +
                                    '<div class="progress-bar progress-bar-danger" role="progressbar"' +
                                    'aria-valuenow="100" aria-valuemin="0" aria-valuemax="100"' +
                                    'style="width: 100%;">' +
                                        //'<span class="sr-only">' + row.percent * 100 + '</span>' +
                                    '<span style="font-size: 8px;">' + (row.percent * 100).toFixed(2) + '%</span>' +
                                    '</div>' +
                                '</div>'
                    } else if(row.percent * 100 > 0 && row.percent * 100 < 30) {
                        return '<div class="progress progress-striped" style="margin-bottom: 0"> ' +
                                    '<div class="progress-bar progress-bar-danger" role="progressbar"' +
                                    'aria-valuenow="'+row.percent * 100+'" aria-valuemin="0" aria-valuemax="100"' +
                                    'style="width: ' + row.percent * 100 + '%;">' +
                                        //'<span class="sr-only">' + row.percent * 100 + '</span>' +
                                    '</div>' +
                                    (row.percent * 100).toFixed(2) + '%' +
                                '</div>'

                    } else if(row.percent * 100 >= 30 && row.percent * 100 < 100) {
                        return '<div class="progress progress-striped" style="margin-bottom: 0"> ' +
                                    '<div class="progress-bar progress-bar-info" role="progressbar"' +
                                    'aria-valuenow="'+row.percent * 100+'" aria-valuemin="0" aria-valuemax="100"' +
                                    'style="width: ' + row.percent * 100 + '%;">' +
                                        //'<span class="sr-only">' + row.percent * 100 + '</span>' +
                                    (row.percent * 100).toFixed(2) + '%' +
                                    '</div>' +
                                '</div>'
                    } else {
                        return '<div class="progress progress-striped" style="margin-bottom: 0"> ' +
                                    '<div class="progress-bar progress-bar-success" role="progressbar"' +
                                    'aria-valuenow="'+row.percent * 100+'" aria-valuemin="0" aria-valuemax="100"' +
                                    'style="width: ' + row.percent * 100 + '%;">' +
                                        //'<span class="sr-only">' + row.percent * 100 + '</span>' +
                                    (row.percent * 100).toFixed(2) + '%' +
                                    '</div>' +
                                '</div>'
                    }

                    //return '<span>' + row.percent * 100 + '% (' + row.hadDone + ' / ' + row.total +  ')</span>';
                }
            },
            {
                target: 7,
                buttons: [
                    {
                        name: '编辑',
                        clazz: 'btn-edit',
                        icon: 'fa-edit',
                        event: function(row){
                            $scope.checkTask = row;
                            //Page.ajaxPostQuiet('checkTask/queryTemplates.do', {taskId : $scope.checkTask.taskId}, function (resp) {
                            //    $scope.updateTemplateList = resp.data;
                            //    $scope.checkTaskAddModal.show();
                            //});
                            $scope.checkTaskAddModal.show();
                        },visiable: function(row) {
                        	return row.status == "unreleased";
                        }
                    },
                    {
                        name: '配置',
                        clazz: 'btn-edit',
                        icon: 'fa-edit',
                        event: function(row){
                            $scope.checkTask = row;
                            Page.ajaxPostQuiet('checkTask/queryTemplates.do', {taskId : $scope.checkTask.taskId}, function (resp) {
                                $scope.updateTemplateList = resp.data;
                                $scope.checkTaskHasSelectModal.show();
                                $scope.checkTaskGrid.taskItemDetail(row);
                                //$scope.checkTaskConfigModal.show();
                            });
                        },visiable: function(row) {
                            return row.status == "unreleased";
                        }
                    },
                    {
                        name: '删除',
                        clazz: 'btn btn-delete',
                        icon: 'fa-trash-o',
                        event: function(row){
                            Page.confirm("您确定要删除该检查任务吗?", function () {
                                Page.ajaxPostQuiet('checkTask/delete.do', row, function () {
                                    $scope.checkTaskGrid.query();
                                });
                            });
                        },visiable: function(row) {
                        	return row.status == "unreleased";
                        }
                    },{
                        name: '发布',
                        clazz: 'btn purple',
                        icon: 'icon-share',
                        event: function(row){
                    		Page.confirm("您确定要发布该检查任务吗?", function () {
                                Page.ajaxPostQuiet('checkTask/release.do', row, function () {
	                                $scope.checkTaskGrid.query();
	                            });
                            });
                        },visiable: function(row) {
                        	return row.status == "unreleased";
                        }
                    },
                    {
                        name: '完结',
                        clazz: 'btn green',
                        icon: 'fa-pause',
                        event: function(row){
                    		Page.confirm("您确定结束该检查任务吗?", function () {
                                Page.ajaxPostQuiet('checkTask/finish.do', row, function () {
	                                $scope.checkTaskGrid.query();
	                            });
                            });
                        },visiable: function(row) {
                        	return row.status == "released";
                        }
                    }
                ]
            }
        ],
        add : function() {
            $scope.checkTask = {};
            $scope.checkTaskAddModal.show();
        },
        taskItemDetail : function(row) {

            //if($("#searchDiv").attr("aria-hidden") == "false") {
            //    $("#searchDiv").css("display","none");
            //}
            $(".searchDiv").each(function(index,data){
                $(data).css("display","none");
            });

            $("#searchTitle").find("a").removeClass("search-collapse").addClass("search-expand");
            $scope.basicItemType = row.taskType;
            $scope.searchTaskId = row.taskId;
            designColDefs(row.taskType);
            //$scope.taskItemModal.show();
        }
    };

    $scope.busCheckGrid = {
        options : {
          lazy : true,
          pageLength: 100
        },
        searchData : function() {
            if(!$scope.busCheckGrid.query.companyId) {
                Page.notice("请选择营运公司!");
                return;
            }

            if(!$scope.busCheckGrid.query.lineNo) {
                Page.notice("请选择线路号!");
                return;
            }
            $scope.busCheckGrid.query();
        },
        reset : function() {
            $scope.busCheckGrid.query.companyId = "";
            $scope.busCheckGrid.query.lineNo = "";
        }
    };

    $scope.stopCheckGrid = {
        options : {
            lazy : true,
            pageLength: 100
        },
        searchData : function() {
            if(!$scope.stopCheckGrid.query.stopName && !$scope.stopCheckGrid.query.roadName) {
                Page.notice("请填写站点名称或街道!");
                return;
            }
            $scope.stopCheckGrid.query();
        },
        reset : function() {
            $scope.stopCheckGrid.query.stopName = "";
            $scope.stopCheckGrid.query.roadName = "";
        }
    };

    $scope.adCheckGrid = {
        options : {
            lazy : true,
            pageLength: 100
        },
        searchData : function() {
            if(!$scope.adCheckGrid.query.billNo && !$scope.adCheckGrid.query.customerName) {
                Page.notice("请填写站点名称或街道!");
                return;
            }

            $scope.adCheckGrid.query();
        },
        reset : function() {
            $scope.adCheckGrid.query.billNo = "";
            $scope.adCheckGrid.query.customerName = "";
        }
    };


    $scope.taskItemGrid = {
        options : {
            bAutoWidth : false
        },
        resultDetail : function(row) {

            //$scope.checkResultModal.taskId = row.taskId;
            Page.ajaxPostQuiet('checkTask/checkResult.do', {taskId : row.taskId, itemId : row.itemId}, function (resp) {
                $scope.checkResultList = resp.data;
                $scope.checkResultModal.show();
            });
        },
        quickQuery : function() {
            $scope.taskItemGrid.query.quickSearch = 1;
            designColDefs($scope.basicItemType);
        },
        checkItemView : function(row) {
            Page.ajaxPostQuiet('checkTask/checkTaskItemMore.do', {taskId : row.taskId, itemId : row.itemId}, function (resp) {
                $scope.checkResultMore = resp.data;
                $scope.checkItemMoreModal.show();
            });
        },
        checkItemDelete : function(row) {

            Page.confirm("确定要删除吗？", function () {
                Page.ajaxPostQuiet('checkTask/checkItemDelete.do', {taskId : row.taskId, itemId : row.itemId}, function (resp) {

                    if($scope.basicItemType == "1") { //车辆检查
                        for (var i = 0; i < $scope.busChecks.length; i++) {
                            var obj = $scope.busChecks[i];
                            if(row.itemId == obj["itemId"]) {
                                $scope.busChecks.splice(i,1);
                            }
                        }
                    } else if($scope.basicItemType == "2") {
                        for (var i = 0; i < $scope.stopChecks.length; i++) {
                            var obj = $scope.stopChecks[i];
                            if(row.itemId == obj["itemId"]) {
                                $scope.stopChecks.splice(i,1);
                            }
                        }
                    } else if($scope.basicItemType == "3") {
                        for (var i = 0; i < $scope.adChecks.length; i++) {
                            var obj = $scope.adChecks[i];
                            if(row.itemId == (obj["itemId"])) {
                                $scope.adChecks.splice(i,1);
                            }
                        }
                    }
                    designColDefs($scope.basicItemType);
                });
            });

        }
    };
    $scope.dynamicQuery = function() {
        var queryTaskType;
        if($scope.taskItemGrid.query.taskType != undefined &&  $scope.taskItemGrid.query.taskType != "") {
            queryTaskType = $scope.taskItemGrid.query.taskType;
        } else {
            queryTaskType = $scope.basicItemType;
        }
        designColDefs(queryTaskType);
    };

    $scope.reset = function() {
        $scope.taskItemGrid.reset();
    };

    $scope.taskItemModal = {
        onOkClick : function() {

        },
        onHidden : function() {

        }
    };

    function designColDefs(taskType) {

        Page.ajaxPostQuiet('checkTask/dynamicHead.do', {taskType : taskType,taskId:$scope.searchTaskId}, function (resp) {
            var column_defs = [
                {
                    buttons: [
                        {
                            name: '查看',
                            class: 'btn-edit',
                            event: 'checkItemView',
                            visiable: function(row) {
                                if(row.status == '1') { //status 为1（已完成）的时候显示
                                    return true;
                                } else {
                                    return false;
                                }
                            }
                        },
                        {
                            name: '删除',
                            class: 'btn-delete',
                            event: 'checkItemDelete',
                            visiable: function(row) {
                                if(row.status == '0') { //status 为1（已完成）的时候显示
                                    return true;
                                } else {
                                    return false;
                                }
                            }
                        }
                    ]
                }
            ];

            column_defs[0].target = resp.data.length - 1;
            $scope.taskItemGrid.column_defs = column_defs;
            $scope.taskItemGrid.setDynamicTable(resp.data,
                {taskType : taskType,taskId:$scope.searchTaskId});
        });
    }

    $scope.checkTaskAddModal = {
        onOkClick : function() {
            if(!$scope.validateForm.$valid) {
                $scope.validateForm.doValidate();
            } else {
                if(!$scope.checkTask.taskId) { //添加
                    //$scope.checkTask.templateIds = angular.extend([],$scope.templateIds.elements);
                    //if($scope.checkTask.templateIds == undefined || $scope.checkTask.templateIds.length == 0) {
                    //    Page.notice("请选择检查项!");
                    //    return;
                    //}
                    Page.ajaxPostQuiet('checkTask/add.do', $scope.checkTask, function (resp) {
                        $scope.checkTaskGrid.query();
                        $scope.checkTaskAddModal.hide();
                        Page.notice("操作成功!");
                    });
                } else {
                    //$scope.checkTask.templateIds = angular.extend([],$scope.templateIds.elements);
                    //if($scope.checkTask.templateIds == undefined || $scope.checkTask.templateIds.length == 0) {
                    //    Page.notice("请选择检查项!");
                    //    return;
                    //}
                    Page.ajaxPostQuiet('checkTask/update.do', $scope.checkTask, function (resp) {
                        $scope.checkTaskGrid.query();
                        $scope.checkTaskAddModal.hide();
                        Page.notice("操作成功!");
                    });
                }
            }
        },
        onHidden : function() {

            if($scope.checkTask.taskId) {
                //维持修改的之前的数据
            } else {
                $scope.templateIds = new Map();
            }
            $scope.validateForm.reset();
        }
    };


    $scope.checkTaskConfigModal = {
        onShow : function() {
            if($scope.checkTask.taskType == "1") {
                if($scope.busCheckGrid.query.companyId != undefined && $scope.busCheckGrid.query.companyId != ""
                    && $scope.busCheckGrid.query.lineNo != undefined && $scope.busCheckGrid.query.lineNo != "") {
                    $scope.busCheckGrid.searchData();
                }
            } else if($scope.checkTask.taskType == "2") {
                if(($scope.stopCheckGrid.query.stopName != undefined && $scope.stopCheckGrid.query.stopName != "")
                    || ($scope.stopCheckGrid.query.roadName != undefined && $scope.stopCheckGrid.query.roadName != "")) {
                    $scope.stopCheckGrid.searchData();
                }
            } else if($scope.checkTask.taskType == "3") {
                if(($scope.adCheckGrid.query.billNo != undefined && $scope.adCheckGrid.query.billNo != "")
                    || ($scope.adCheckGrid.query.customerName != undefined && $scope.adCheckGrid.query.customerName != "")) {
                    $scope.adCheckGrid.searchData();
                }
            }

        },
        onOkClick : function() {
            var checkList;
            if($scope.basicItemType == "1") { //车辆检查
                checkList = angular.extend([],$scope.busChecks);
            } else if($scope.basicItemType == "2") {
                checkList = angular.extend([],$scope.stopChecks);
            } else if($scope.basicItemType == "3") {
                checkList = angular.extend([],$scope.adChecks);
            }
            if(checkList.length > 0) {
                Page.ajaxPostQuiet('checkTask/saveSelectItem.do', {taskType : $scope.checkTask.taskType,
                    checkItem : checkList, taskId : $scope.checkTask.taskId}, function (resp) {
                    Page.notice("保存成功!");
                    designColDefs($scope.checkTask.taskType);
                    $scope.checkTaskConfigModal.hide();
                });
            }
        },
        onHidden : function() {

        },
        checkTemplate : function(event,template) {
            if(event.target.checked) {
                $scope.templateIds.put(template.templateId,template.templateId);
            } else {
                $scope.templateIds.remove(template.templateId);
            }
        }
    };

    $scope.checkTaskHasSelectModal = {
        onShow : function() {
            Page.ajaxPostQuiet('template/query.do', {}, function (resp) {
                $scope.templateList = resp.data.rows;
                $scope.templateIds = new Map();
                for (var i = 0; i < $scope.updateTemplateList.length; i++) {
                    $scope.templateIds.put($scope.updateTemplateList[i].templateId,$scope.updateTemplateList[i].templateId);
                    for (var j = 0; j < $scope.templateList.length; j++) {
                        if($scope.updateTemplateList[i].templateId == $scope.templateList[j].templateId) {
                            $scope.templateList[j].showChecked = true;
                            break;
                        }
                    }
                }
            });
        },
        onOkClick : function() {

            $scope.checkTask.templateIds = angular.extend([],$scope.templateIds.elements);
            if($scope.checkTask.templateIds == undefined || $scope.checkTask.templateIds.length == 0) {
                Page.notice("请选择检查项!");
                return;
            }
            var hasSelectItemList;
            if($scope.checkTask.taskType == "1") {
                hasSelectItemList = $scope.busChecks;
            } else if($scope.checkTask.taskType == "2") {
                hasSelectItemList = $scope.stopChecks;
            } else if($scope.checkTask.taskType == "3") {
                hasSelectItemList = $scope.adChecks;
            }
            if(hasSelectItemList == undefined || hasSelectItemList.length == 0) {
                //不在models中，则在查询一次确认是否存在检查数据
                Page.ajaxPostQuiet('checkTask/dynamicData4ReShow.do', {taskId : $scope.checkTask.taskId,
                    taskType : $scope.checkTask.taskType}, function (resp) {
                    hasSelectItemList = resp.data;
                    if(hasSelectItemList == undefined || hasSelectItemList.length == 0) {
                        Page.notice("请选择检查数据!");
                        return;
                    } else {
                        Page.ajaxPostQuiet('checkTask/saveTemplates.do', {templateIds : $scope.checkTask.templateIds,
                            hasSelectItemList : hasSelectItemList,taskId : $scope.checkTask.taskId}, function (resp) {
                            Page.notice("操作成功!");
                            $scope.checkTaskHasSelectModal.hide();
                        });
                    }
                });
            } else {
                Page.ajaxPostQuiet('checkTask/saveTemplates.do', {templateIds : $scope.checkTask.templateIds,
                    hasSelectItemList : hasSelectItemList,taskId : $scope.checkTask.taskId}, function (resp) {
                    Page.notice("操作成功!");
                    $scope.checkTaskHasSelectModal.hide();
                });
            }



        },
        onHidden : function() {

        },
        selectData : function() {

            var taskId = $scope.checkTask.taskId;
            var taskType = $scope.checkTask.taskType;
            //if(taskType == "1") { //车辆
            //    $scope.busCheckGrid.query.companyId = "";
            //    $scope.busCheckGrid.query.lineNo = "";
            //} else if(taskType == "2") { //站点
            //    $scope.stopCheckGrid.query = {};
            //} else if(taskType == "3") { //广告
            //    $scope.adCheckGrid.query = {};
            //}
            Page.ajaxPostQuiet('checkTask/dynamicData4ReShow.do', {taskId : taskId, taskType : taskType}, function (resp) {

                if(resp.data != undefined && resp.data.length > 0 ) {
                    if(taskType == "1") { //车辆
                        $scope.busChecks = resp.data;
                    } else if(taskType == "2") { //站点
                        $scope.stopChecks = resp.data;
                    } else if(taskType == "3") { //广告
                        $scope.adChecks = resp.data;
                    }
                }
                $scope.checkTaskConfigModal.show();
            });
        },
        checkTemplate : function(event,template) {
            if(event.target.checked) {
                $scope.templateIds.put(template.templateId,template.templateId);
            } else {
                $scope.templateIds.remove(template.templateId);
            }
        }

    };

    $scope.checkResultModal = {
        onShow : function() {

        },
        onOkClick : function() {

        },
        onHidden : function() {

        }
    };

}]);
